<?php
class Img{
	private $name;
	private $type;
	private $tmp_name;
	private $size;
	private $path = "/uploads";
	private $ckPath = "/uploads/ckeditor";
	private $fileTypes = array('jpg','jpeg','gif','png','x-png');

	private $maxSize = '2048';//上传图片最大值，单位为KB
	private $photoType = '0';

	private $filePath;
	private $thumbPath;
	private $fileName;
	private $tmpImg;
	private $thumbWidth = 232;
	private $thumbHeight = 232;
	private $bigPath;
	public function __construct($imgInfo,$photoType=0){
		$this->photoType = $photoType;
		$this->name = $imgInfo['name'];
		$this->type = $imgInfo['type'];
		$this->tmp_name = $imgInfo['tmp_name'];
		$this->size = $imgInfo['size'];
		$this->filePath = $_SERVER['DOCUMENT_ROOT'].rtrim($this->path,'/').'/'.date('Ymd');
		$this->thumbPath = rtrim($this->filePath,'/').'/thumb';
		$this->bigPath = rtrim($this->filePath,'/').'/big';
		$this->fileName = time().rand(0,9).strrchr($this->name,'.');
	}
	public function saveUpload(){
		rare_mkdir($this->filePath);
		$targetFile = rtrim($this->filePath,'/').'/'.$this->fileName;
		$fileParts = pathinfo($this->name);
		if($this->size>($this->maxSize*1024*1024)){
			return "上传的文件不能超过".$this->maxSize."MB!";
		}
		if (in_array($fileParts['extension'],$this->fileTypes)) {
			move_uploaded_file($this->tmp_name,$targetFile);
			if($this->photoType==service_type::PHOTO){
				$this->createThumb(600,450);
			}elseif($this->photoType==service_type::NEWS){
				$this->createBig(309,162);	
				$this->createThumb($this->thumbWidth,$this->thumbHeight);
			}else{
				$this->createThumb($this->thumbWidth,$this->thumbHeight);
			}
			return substr($targetFile,strlen($_SERVER['DOCUMENT_ROOT']));
		} else {
			return 'Invalid file type.';
		}
	}	
	private function createBig($tWidth,$tHeight){
		$this->init_img();
		rare_mkdir($this->bigPath);
		$width = imagesx($this->tmpImg);
		$height = imagesy($this->tmpImg);
		$thumbWidth = $tWidth;
		$thumbHeight = $tHeight;
		$ratio = $width/$height;
		$resize_ratio = $thumbWidth/$thumbHeight;
		if($ratio>=$resize_ratio) 
		{
			$newimg = imagecreatetruecolor($thumbWidth,($thumbWidth)/$ratio); 
			imagecopyresampled($newimg, $this->tmpImg, 0, 0, 0, 0, $thumbWidth, ($thumbWidth)/$ratio, $width, $height); 
			ImageJpeg ($newimg,rtrim($this->bigPath,'/').'/'.$this->fileName); 
		} 
		if($ratio<$resize_ratio) 
		{ 
			$newimg = imagecreatetruecolor(($thumbHeight)*$ratio,$thumbHeight); 
			imagecopyresampled($newimg, $this->tmpImg, 0, 0, 0, 0, ($thumbHeight)*$ratio, $thumbHeight, $width, $height); 
			ImageJpeg ($newimg,rtrim($this->bigPath,'/').'/'.$this->fileName); 
		}
	} 
	private function createThumb($tWidth,$tHeight){
		$this->init_img();
		rare_mkdir($this->thumbPath);
		$width = imagesx($this->tmpImg);
		$height = imagesy($this->tmpImg);
		$thumbWidth = $tWidth;
		$thumbHeight = $tHeight;
		$ratio = $width/$height;
		$resize_ratio = $thumbWidth/$thumbHeight;
		$newimg = imagecreatetruecolor($thumbWidth,$thumbHeight);
		$white = @imagecolorallocate($newimg, 255, 255, 255);
		imagefill($newimg,0,0,$white);
		if($ratio>=$resize_ratio){
			imagecopyresampled($newimg, $this->tmpImg, 0, ($thumbHeight-($thumbWidth/$ratio))/2, 0, 0, $thumbWidth, ($thumbWidth)/$ratio, $width, $height); 
			ImageJpeg ($newimg,rtrim($this->thumbPath,'/').'/'.$this->fileName); 
		} 
		if($ratio<$resize_ratio){ 
			imagecopyresampled($newimg, $this->tmpImg, ($thumbWidth-($thumbHeight*$ratio))/2, 0, 0, 0, ($thumbHeight)*$ratio, $thumbHeight, $width, $height); 
			ImageJpeg ($newimg,rtrim($this->thumbPath,'/').'/'.$this->fileName); 
		}
	} 
	private function init_img(){
		$targetFile = rtrim($this->filePath,'/').'/'.$this->fileName;
		$fileParts = pathinfo($this->name);
		$type = $fileParts['extension'];
		if($type=="jpg" || $type=='jpeg'){
			$this->tmpImg = imagecreatefromjpeg($targetFile); 
		}elseif($type=="gif"){ 
			$this->tmpImg = imagecreatefromgif($targetFile); 
		}elseif($type=="png" || $type=='x-png'){
			$this->tmpImg = imagecreatefrompng($targetFile); 
		}
	} 
	public function saveCKUpload(){

		$filearr=pathinfo($this->name); 
		$filetype=$filearr["extension"]; 
		if(!in_array($filetype,$this->fileTypes)){
			return array('msg'=>"错误的文件类型!");
		} 
		if($_FILES['upload']['size']>$this->maxSize*1024){
			return array('msg'=>"上传的文件不能超过".$this->maxSize."KB!");
		}
		$filePath = $_SERVER['DOCUMENT_ROOT'].rtrim($this->ckPath,'/').'/';
		rare_mkdir($filePath);
		$targetFile = rtrim($filePath,'/').'/'.$this->fileName;
		if(move_uploaded_file($this->tmp_name,$targetFile)){
			return array('file'=>substr($targetFile,strlen($_SERVER['DOCUMENT_ROOT'])),'msg'=>'上传成功!');
		}else{
			return array('msg'=>'文件上传失败，请检查上传目录设置和目录读写权限');
		} 
	}
}
?>
